<template>
  <div id="qiu_login">
    <!-- <div id="login_box"></div> -->
    <div id="login_item">
      <div>
        <h1 id="logo_name">秋</h1>
      </div>
      <h3>欢迎使用秋管理系统</h3>
      <div class="item">
        <div class="group">
          <input
            type="text"
            class="admin"
            placeholder="用户名"
            v-model.trim="names"
          />
        </div>
        <div class="group">
          <input
            :type="typePwd"
            class="admin"
            placeholder="密码"
            v-model.trim="pwd"
          />
          <img :src="eyes ? by : ky" @click="eyesClick" />
        </div>
      </div>
      <div class="text-center">
        <button id="login_btn" @click="verifyName">登录</button>
      </div>
      <div class="text-a">
        <span>
          <a href="">忘记密码了?</a>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex"
export default {
  data() {
    return {
      names: "",
      pwd: "",
      eyes: true,
      typePwd: "password",
      by: require("@/assets/by.svg"),
      ky: require("@/assets/ky.svg"),
    }
  },
  methods: {
    // mutations 是方法, 所以要注入到组件的方法里
    ...mapMutations(["updateUname"]),

    //向数据库发起请求验证用户名和密码
    verifyName() {
      let bname = this.names
      let bpwd = this.pwd
      this.axios.post("/login", `bname=${bname}&bpwd=${bpwd}`).then((res) => {
        if (res.data.code == 200) {
          this.updateUname(this.names)
          sessionStorage.setItem("uname", this.names)
          this.$router.push("/home")
        } else {
          this.$message.error("提示:账号密码输入错误", 1)
        }
      })
    },
    //密码明文
    eyesClick() {
      if (this.eyes) {
        this.eyes = false
        this.typePwd = "text"
      } else {
        this.eyes = true
        this.typePwd = "password"
      }
    },
  },
}
</script>
<style src="../assets/css/general.css" scoped></style>
<style scoped>
.group {
  position: relative;
}
.group img {
  position: absolute;
  top: 9px;
  right: 35px;
  cursor: pointer;
}
</style>
